package com.linkhub.oms.dao.mpservice.impl;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkhub.oms.common.enums.mp.PickUpOrderStatusEnum;
import com.linkhub.oms.dao.mapper.PickUpGoodsOrderEntityMapper;
import com.linkhub.oms.dao.mpservice.IPickUpGoodsOrderService;
import com.linkhub.oms.entity.PickUpGoodsOrderEntity;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 提货申请表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-04-30
 */
@Service
public class PickUpGoodsOrderServiceImpl extends ServiceImpl<PickUpGoodsOrderEntityMapper, PickUpGoodsOrderEntity> implements IPickUpGoodsOrderService {

    @Override
    public PickUpGoodsOrderEntity queryBySysEntryOrderCode(String sysEntryOrderCode) {

        return lambdaQuery()
                .eq(PickUpGoodsOrderEntity::getSysEntryOrderCode, sysEntryOrderCode)
                .one();
    }

    @Override
    public PickUpGoodsOrderEntity getOne(String sysEntryOrderCode, SFunction<PickUpGoodsOrderEntity, ?>... columns) {

        return lambdaQuery()
                .select(columns)
                .eq(PickUpGoodsOrderEntity::getSysEntryOrderCode, sysEntryOrderCode)
                .one();
    }

    @Override
    public void cancel(String sysEntryOrderCode) {
        this.changeState(sysEntryOrderCode, PickUpOrderStatusEnum.CANCEL);
    }

    @Override
    public void changeState(String sysEntryOrderCode, PickUpOrderStatusEnum state) {
        lambdaUpdate()
                .eq(PickUpGoodsOrderEntity::getSysEntryOrderCode, sysEntryOrderCode)
                .set(PickUpGoodsOrderEntity::getState, state)
                .update();
    }

    @Override
    public boolean changeState(String sysEntryOrderCode, PickUpOrderStatusEnum beforeState, PickUpOrderStatusEnum state) {
        return lambdaUpdate()
                .eq(PickUpGoodsOrderEntity::getSysEntryOrderCode, sysEntryOrderCode)
                .eq(Objects.nonNull(beforeState), PickUpGoodsOrderEntity::getState, beforeState)
                .set(PickUpGoodsOrderEntity::getState, state)
                .update();
    }

    @Override
    public void updateNullById(Long id, SFunction<PickUpGoodsOrderEntity, ?>... columns) {
        if (Objects.isNull(columns)) {
            return;
        }
        LambdaUpdateChainWrapper<PickUpGoodsOrderEntity> updateWrapper = lambdaUpdate()
                .eq(PickUpGoodsOrderEntity::getId, id);
        for (SFunction<PickUpGoodsOrderEntity, ?> column : columns) {
            updateWrapper.set(column, null);
        }

        updateWrapper.update();


    }

}
